package com.heima.wemedia.service.impl;

import com.heima.apis.schedule.IScheduleClient;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.schedule.dto.Task;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.utils.ProtostuffUtil;
import com.heima.wemedia.service.WmNewsTaskService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Slf4j
@Transactional
public class WmNewsTaskServiceImpl implements WmNewsTaskService {



    @Autowired
    private IScheduleClient scheduleClient;



    /**
     * 添加任务到延迟队列中
     * @param newsId  文章的id
     * @param executeTime  发布的时间  可以做为任务的执行时间
     */
    @Override
    public Boolean addNewsToTask(Integer newsId, Long executeTime) {

        Task task = new Task();
        task.setExecuteTime(executeTime);
        WmNews wmNews = new WmNews();
        wmNews.setId(newsId);

        task.setParameters(ProtostuffUtil.serialize(wmNews));
        //// TODO: 2022/7/29  进不去
        ResponseResult responseResult = scheduleClient.addTask(task);
        if (responseResult != null && responseResult.getCode() == 200){
            return true;
        }

        return false;

    }


}
